package com.skateboard.reelviewtest

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.LinearInterpolator
import android.view.animation.ScaleAnimation
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import com.skateboard.reelviewtest.rotate.Rotate3dAnimation
import kotlinx.android.synthetic.main.activity_test3d_rotate.*

class Test3dRotateActivity : AppCompatActivity() {
    var rootView: RelativeLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test3d_rotate)
        rootView = findViewById(R.id.rootView)
    }

    private fun test() {
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        val rotateAnimation1 = Rotate3dAnimation(application, 0f, 360f,
                (rootView!!.measuredWidth / 2).toFloat(), (rootView!!.measuredHeight / 2).toFloat(), 1f, true)
        rotateAnimation1.duration = 2500
        rotateAnimation1.fillAfter = true
        rotateAnimation1.interpolator = LinearInterpolator()
        rotateAnimation1.setRotateDegreesCallback { degress ->
            if (degress in 90.0..270.0) {
                if(iv_back.visibility!=View.VISIBLE){
                    iv_back.visibility= View.VISIBLE
                    iv_foreground.visibility=View.INVISIBLE
                }
            } else {
                if(iv_back.visibility!=View.INVISIBLE){
                    iv_back.visibility= View.INVISIBLE
                    iv_foreground.visibility=View.VISIBLE
                }
            }
        }

        val scaleAniX = ObjectAnimator.ofFloat(rootView, "scaleX", 1f, 2.5f)

        val scaleAniY  = ObjectAnimator.ofFloat(rootView, "scaleY", 1f, 2.5f)

        var scaleSet=AnimatorSet();
        scaleSet.duration=4000
        scaleSet.play(scaleAniX).with(scaleAniY)

        rootView!!.startAnimation(rotateAnimation1)
        scaleSet.start()
    }
    private fun test1() {
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        val rotateAnimation1 = Rotate3dAnimation(application, 0f, 360f,
                (rootView!!.measuredWidth / 2).toFloat(), (rootView!!.measuredHeight / 2).toFloat(), 1f, true)
        //rotateAnimation1.duration = 3000
        rotateAnimation1.fillAfter = true
        rotateAnimation1.interpolator = LinearInterpolator()
        rotateAnimation1.setRotateDegreesCallback { degress ->
            if (degress in 90.0..270.0) {
                if(iv_back.visibility!=View.VISIBLE){
                    iv_back.visibility= View.VISIBLE
                    iv_foreground.visibility=View.INVISIBLE
                }
            } else {
                if(iv_back.visibility!=View.INVISIBLE){
                    iv_back.visibility= View.INVISIBLE
                    iv_foreground.visibility=View.VISIBLE
                }
            }
        }


        var scaleAnimation = ScaleAnimation(1f, 2f, 1f, 2f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f)





        var set=AnimationSet(true)
        set.addAnimation(rotateAnimation1)
        set.addAnimation(scaleAnimation)
        set.duration=3000
        set.fillAfter=true
        rootView!!.startAnimation(set)
    }
    override fun onTouchEvent(event: MotionEvent): Boolean {
        test()
        return super.onTouchEvent(event)
    }
}